package com.ddwl.gate.service;

import com.ddwl.common.constant.GateEnum;
import com.ddwl.common.exception.CommonException;
import com.ddwl.common.exception.ExceptionCode;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.ObjectUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.gate.mapper.AppReleaseMapper;
import com.ddwl.gate.model.AppRelease;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.gate.AppReleaseBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.gate.AppReleaseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Fu Zhaohui
 * @date 2017/4/21
 */
@Service
@RequiredArgsConstructor
public class AppService {

    private final AppReleaseMapper appReleaseMapper;

    public AppReleaseVo getAppVersion(AppReleaseBo bo) {
        Example example = new Example(AppRelease.class);
        example.and().andEqualTo("product", bo.getProduct())
                .andEqualTo("platform", bo.getPlatform())
                .andEqualTo("status", GateEnum.AppStatus.ONLINE.getCode());
        if (bo.getVersionCode() != null) {
            example.and().andGreaterThan("versionCode", bo.getVersionCode());
        }
        example.orderBy("id").desc();
        PageHelper.startPage(1, 10).count(false);
        List<AppRelease> entities = appReleaseMapper.selectByExample(example);
        if (!entities.isEmpty()) {
            Long forceCount = entities.stream().filter(entity -> entity.getForce()).count();
            AppRelease entity = entities.get(0);
            AppReleaseVo vo = new AppReleaseVo();
            BeanUtils.copyProperties(entity, vo);
            vo.setForce(forceCount > 0);
            return vo;
        }
        return null;
    }

    @Transactional(rollbackFor = Throwable.class)
    public String create(AppReleaseBo bo) {
        AppRelease entity = new AppRelease();
        BeanUtils.copyProperties(bo, entity);
        entity.setId(UUIDGenerator.getShortUUID());
        appReleaseMapper.insertSelective(entity);
        return entity.getId();
    }

    @Transactional(rollbackFor = Throwable.class)
    public void update(AppReleaseBo bo) {
        AppRelease entity = appReleaseMapper.selectByPrimaryKey(bo.getId());
        if (entity == null) {
            throw new CommonException(ExceptionCode.DATA_NOT_EXISTS);
        }
        BeanUtils.copyProperties(bo, entity, ObjectUtil.getNullFields(bo));
        appReleaseMapper.updateByPrimaryKeySelective(entity);
    }

    public PageVo<AppReleaseVo> page(PageBo<AppReleaseBo> pageBo) {
        PageVo<AppReleaseVo> pageVo = new PageVo<>();
        BeanUtils.copyProperties(pageBo, pageVo);
        pageVo.setSize(pageBo.getPageSize());
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize()).setOrderBy("id desc");
        Page<AppRelease> pageable = (Page<AppRelease>) appReleaseMapper.select(BeanConverUtils.convertBean(pageBo.getParam(), AppRelease.class));
        pageVo.setTotal(pageable.getTotal());
        pageVo.setData(pageable.getResult().stream().map(entity -> {
            AppReleaseVo vo = new AppReleaseVo();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList()));
        return pageVo;
    }

    @Transactional(rollbackFor = Throwable.class)
    public void delete(String id) {
        appReleaseMapper.deleteByPrimaryKey(id);
    }
}
